/*
 * @Author: hongbin
 * @Date: 2023-06-22 13:04:04
 * @LastEditors: hongbin
 * @LastEditTime: 2023-06-22 16:41:29
 * @Description: 端午粽子
 */
import { FC } from "react";
import * as THREE from "three";
import { ThreeHelper } from "@/ThreeHelper";
import Layout from "@/components/Three/Layout";

interface IProps {}

const destroyEvent = [] as VoidFunction[];

const Index: FC<IProps> = () => {
    return (
        <Layout
            seoTitle={"端午安康"}
            init={init}
            destroy={() => destroyEvent.forEach((destroy) => destroy())}
        />
    );
};

export default Index;

async function init(helper: ThreeHelper) {
    helper.addStats();
    helper.camera.position.set(0, 5, 10);
    helper.frameByFrame();
    helper.addGUI();
    helper.addAxis();
    helper.useRoomEnvironment(true);

    // const hemisphereLight = new THREE.HemisphereLight(0xdddddd, 0xffffff, 0.8);
    // hemisphereLight.position.set(0, -2, 110);
    // const directionalLight = new THREE.DirectionalLight(0xffffff, 0.45);

    // helper.add(hemisphereLight, directionalLight);
    // const pointLight = new THREE.PointLight("#fff", 2, 20);
    // const pointLight = new THREE.PointLight("#fff", 2, 20);
    // helper.camera.add(pointLight);
    // helper.add(helper.camera);

    const zongZiShader = new ZongZiShader();

    const plane = new THREE.Mesh(
        new THREE.PlaneGeometry(Math.PI * 2, Math.PI, 50, 50),
        zongZiShader.material
    );

    helper.add(plane);
}

class ZongZiShader {
    material = new THREE.MeshStandardMaterial({
        side: THREE.DoubleSide,
        metalness: 1,
        map: new THREE.TextureLoader().load(
            "/textures/leafy_grass_diff_1k.jpg"
        ),
    });

    constructor() {
        this.material.onBeforeCompile = (shader) => {
            shader.vertexShader = shader.vertexShader.replace(
                "#include <project_vertex>",
                `
                vec2 vUv = uv * vec2(3.1415926 * 2.,3.1415926);
                transformed.x = cos(vUv.x);
                transformed.y = cos(vUv.y);
                transformed.z = cos(vUv.y + vUv.x);
                #include <project_vertex>
            `
            );

            shader.vertexShader = shader.vertexShader.replace(
                "#include <beginnormal_vertex>",
                `
                #include <beginnormal_vertex>
                vec2 myUv = uv * vec2(3.1415926 * 2.,3.1415926);

                objectNormal.x = cos(myUv.y);
                objectNormal.y = cos(myUv.x);
                objectNormal.z = cos(myUv.y + myUv.x);
            `
            );
        };
    }
}
